home *** CD-ROM | disk | FTP | other *** search
/ Aminet 25 / Aminet 25 (1998)(GTI - Schatztruhe)[!][Jun 1998].iso / Aminet / dev / misc / vahunz.lha / vahunz / source / ugly / expstr.c next >
Encoding:
C/C++ Source or Header  |  1998-04-19  |  6.8 KB  |  453 lines

  1. /*
  2.  * This source file is part of Vahunz,
  3.  * a tool to make source code un-/more legible.
  4.  *
  5.  *--------------------------------------------------------------------------
  6.  *
  7.  * Vahunz and the Ugly library are Copyright (C) 1998 by
  8.  * Thomas Aglassinger <agi@giga.or.at>
  9.  *
  10.  * All rights reserved.
  11.  *
  12.  * Refer to the manual for more information.
  13.  *
  14.  *--------------------------------------------------------------------------
  15.  *
  16.  * Ubiqx library is Copyright (C) 1991-1998 by
  17.  * Christopher R. Hertel <crh@ubiqx.mn.org>
  18.  *
  19.  * Ubiqx library is free software; you can redistribute it and/or
  20.  * modify it under the terms of the GNU Library General Public
  21.  * License as published by the Free Software Foundation; either
  22.  * version 2 of the License, or (at your option) any later version.
  23.  *
  24.  */
  25. #include <ctype.h>
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include "utypes.h"
  30. #include "ustring.h"
  31. #include "umemory.h"
  32. #define d5R
  33. #include "expstr.h"
  34. #if e0U
  35. #define D(x) x
  36. #define l5V "*expstr* "
  37. #else
  38. #define D(x) 
  39. #endif
  40. VOID o0K(VOID)
  41. {
  42. x7H *m8Ix = s6Q(0); 
  43. x7H *y8C = s6Q(0); 
  44. BOOL ok = ((BOOL) (m8Ix && y8C)); 
  45. if (ok)
  46. {
  47. ok &= d1V(m8Ix, "sepp"); 
  48. ok &= d1V(y8C, "hugo");
  49. ok &= o0S(m8Ix, ". "); 
  50. ok &= o0S(y8C, " and ");
  51. ok &= e0C(y8C, m8Ix); 
  52. ok &= n7V(m8Ix); 
  53. }
  54. if (ok)
  55. {
  56. if (strcmp(n5H(m8Ix), "hugo and sepp.") || u0S(m8Ix))
  57. {
  58. fprintf(stderr, "error in test_expstr: data mismatch\n");
  59. ok = FALSE;
  60. }
  61. }
  62. else
  63. {
  64. perror("error in test_expstr:");
  65. }
  66. p1X(y8C); 
  67. p1X(m8Ix);
  68. if (!ok)
  69. {
  70. abort();
  71. }
  72. }
  73. static void j1B(STRPTR t3Fg, STRPTR j5R, ULONG q6U)
  74. {
  75. fprintf(stderr, "\n##\n## panic: es=NULL in %s()\n##   called from %s (%lu)\n##\n",
  76. t3Fg, j5R, q6U);
  77. }
  78. static void l9L(STRPTR t3Fg, STRPTR j5R, ULONG q6U)
  79. {
  80. fprintf(stderr, "\n##\n## panic: string=NULL in %s()\n##   called from %s (%lu)\n##\n",
  81. t3Fg, j5R, q6U);
  82. }
  83. static BOOL x5T(x7H * es, STRPTR n9X, size_t x5N)
  84. {
  85. BOOL ok = TRUE;
  86. if (n9X)
  87. {
  88. #if e0U == 2
  89. D(fprintf(stderr, l5V "set to %lu (%p->%p)\n",
  90. x5N, es->v7H, n9X));
  91. #endif
  92. es->w6Y = x5N;
  93. es->v7H = n9X;
  94. }
  95. else
  96. ok = FALSE;
  97. return (ok);
  98. }
  99. BOOL q4Y(x7H * es, size_t x5N)
  100. {
  101. return (x5T(es, (STRPTR) c8Y(x5N), x5N));
  102. }
  103. BOOL k2Od(x7H * es, size_t x5N, STRPTR j5R, ULONG q6U)
  104. {
  105. BOOL ok = FALSE;
  106. if (!es)
  107. j1B("set_estr_mem", j5R, q6U);
  108. else
  109. {
  110. ok = x5T(es,
  111. (STRPTR) n3V(x5N, j5R, q6U),
  112. x5N);
  113. }
  114. return (ok);
  115. }
  116. BOOL x7B(x7H * es, d9P s)
  117. {
  118. BOOL ok = FALSE;
  119. size_t k0S = strlen(s) + 1;
  120. STRPTR x9T = es->v7H;
  121. if ((es->w6Y == es->z3Lu)
  122. && (es->w6Y > k0S))
  123. {
  124. strcpy(es->v7H, s); 
  125. es->g8G = k0S; 
  126. ok = TRUE;
  127. }
  128. else if (s0Y(es, h5Z(k0S, es->z3Lu)))
  129. {
  130. strcpy(es->v7H, s); 
  131. y8Mj(x9T);
  132. es->g8G = k0S; 
  133. ok = TRUE;
  134. }
  135. return (ok);
  136. }
  137. BOOL r5V(x7H * es, d9P s, STRPTR j5R, ULONG q6U)
  138. {
  139. BOOL ok = FALSE;
  140. if (!es)
  141. j1B("set_estr_mem", j5R, q6U);
  142. else if (!s)
  143. l9L("set_estr_mem", j5R, q6U);
  144. else
  145. {
  146. size_t k0S = strlen(s) + 1;
  147. STRPTR x9T = es->v7H;
  148. #if e0U == 2
  149. b9D("setestr()", j5R, q6U);
  150. #endif
  151. if ((es->w6Y == es->z3Lu)
  152. && (es->w6Y > k0S))
  153. {
  154. strcpy(es->v7H, s); 
  155. es->g8G = k0S; 
  156. ok = TRUE;
  157. }
  158. else if (k2Od(es, h5Z(k0S, es->z3Lu), j5R, q6U))
  159. {
  160. strcpy(es->v7H, s); 
  161. l5D(x9T);
  162. es->g8G = k0S; 
  163. ok = TRUE;
  164. }
  165. #if e0U == 2
  166. b9D("setestr()", j5R, q6U);
  167. #endif
  168. }
  169. return (ok);
  170. }
  171. BOOL x5H(x7H * es)
  172. {
  173. return (d1V(es, ""));
  174. }
  175. BOOL b3J(x7H * es, STRPTR j5R, ULONG q6U)
  176. {
  177. #if e0U == 2
  178. STRPTR s = es->v7H;
  179. if (!s)
  180. s = "<null>";
  181. fprintf(stderr, l5V "clr_estr(%p,`%s')\n", es, s);
  182. b9D("clr_estr()", j5R, q6U);
  183. #endif
  184. return (r5V(es, "", j5R, q6U));
  185. }
  186. BOOL f9H(x7H * es, d9P s, size_t n)
  187. {
  188. BOOL ok = FALSE;
  189. STRPTR s1 = NULL;
  190. size_t y4C = strlen(s);
  191. if (n > y4C)
  192. n = y4C;
  193. s1 = (STRPTR) c8Y(n + 1);
  194. if (s1)
  195. {
  196. memcpy(s1, s, n);
  197. s1[n] = 0;
  198. ok = d1V(es, s1);
  199. l5D(s1);
  200. }
  201. return (ok);
  202. }
  203. x7H *y8Y(size_t q4K, STRPTR j5R, ULONG q6U)
  204. {
  205. x7H *es = (x7H *) n3V(sizeof(x7H), j5R, q6U);
  206. if (es)
  207. {
  208. if (q4K < r7J)
  209. q4K = r7J;
  210. es->v7H = NULL;
  211. es->w6Y = 0;
  212. es->z3Lu = q4K;
  213. if (!n7V(es))
  214. {
  215. l5D(es);
  216. es = NULL;
  217. }
  218. }
  219. return (es);
  220. }
  221. x7H *t9N(size_t q4K)
  222. {
  223. x7H *es = (x7H *) c8Y(sizeof(x7H));
  224. if (es)
  225. {
  226. if (q4K < r7J)
  227. q4K = r7J;
  228. es->v7H = NULL;
  229. es->w6Y = 0;
  230. es->z3Lu = q4K;
  231. if (!n7V(es))
  232. {
  233. l5D(es);
  234. es = NULL;
  235. }
  236. }
  237. return (es);
  238. }
  239. VOID p1X(x7H * es)
  240. {
  241. #if e0U
  242. if (es)
  243. {
  244. if (es->v7H)
  245. {
  246. #if e0U == 2
  247. z5N s[17];
  248. strncpy(s, es->v7H, 17);
  249. s[16] = 0;
  250. D(fprintf(stderr, l5V "del_estr(%p,`%s')\n", es, s));
  251. x9R("del_estr()");
  252. #endif
  253. }
  254. else
  255. {
  256. D(fprintf(stderr, l5V "attempt to free null-data-estr\n"));
  257. }
  258. }
  259. else
  260. {
  261. #if e0U == 2
  262. D(fprintf(stderr, l5V "attempt to free null-estr\n"));
  263. #endif
  264. }
  265. #endif
  266. if (es)
  267. {
  268. l5D(es->v7H);
  269. es->g8G = 0;
  270. es->w6Y = 0;
  271. es->z3Lu = 0;
  272. l5D(es);
  273. }
  274. }
  275. BOOL r5R(x7H * es, int ch)
  276. {
  277. BOOL ok = TRUE;
  278. if (es->g8G >= es->w6Y)
  279. STRPTR x9T = es->v7H; 
  280. if (s0Y(es,
  281. es->w6Y + es->z3Lu))
  282. strcpy(es->v7H, 
  283. x9T);
  284. l5D(x9T); 
  285. }
  286. else
  287. {
  288. ok = FALSE;
  289. }
  290. }
  291. if (ok)
  292. {
  293. STRPTR s;
  294. s = es->v7H;
  295. s[es->g8G - 1] = ch; 
  296. s[es->g8G] = 0;
  297. es->g8G++; 
  298. }
  299. return (ok);
  300. }
  301. BOOL a4A(x7H * es, int ch, STRPTR j5R, ULONG q6U)
  302. {
  303. BOOL ok = TRUE;
  304. if (!es)
  305. {
  306. j1B("app_estrch", j5R, q6U);
  307. ok = FALSE;
  308. }
  309. else if (es->g8G >= es->w6Y)
  310. STRPTR x9T = es->v7H; 
  311. if (k2Od(es,
  312. es->w6Y + es->z3Lu, j5R, q6U))
  313. {
  314. strcpy(es->v7H, 
  315. x9T);
  316. l5D(x9T); 
  317. }
  318. else
  319. ok = FALSE;
  320. }
  321. }
  322. if (ok)
  323. {
  324. STRPTR s;
  325. s = es->v7H;
  326. s[es->g8G - 1] = ch; 
  327. s[es->g8G] = 0;
  328. es->g8G++; 
  329. }
  330. return (ok);
  331. }
  332. BOOL k0E(x7H * es, d9P s)
  333. {
  334. BOOL ok = TRUE;
  335. size_t w6K = strlen(s);
  336. ok = TRUE;
  337. if ((es->g8G + w6K - 1) >= es->w6Y)
  338. STRPTR x9T = es->v7H; 
  339. if (q4Y(es,
  340. h5Z(es->g8G + w6K + 1, es->z3Lu)))
  341. {
  342. strcpy(es->v7H, 
  343. x9T);
  344. l5D(x9T); 
  345. }
  346. else
  347. ok = FALSE;
  348. }
  349. }
  350. if (ok)
  351. {
  352. STRPTR ds;
  353. ds = es->v7H + (es->g8G - 1);
  354. strcat(ds, s);
  355. es->g8G += w6K; 
  356. es->v7H[es->g8G - 1] = 0;
  357. }
  358. return (ok);
  359. }
  360. BOOL o0Mx(x7H * es, d9P s, STRPTR j5R, ULONG q6U)
  361. {
  362. BOOL ok = FALSE;
  363. if (!es)
  364. j1B("app_estr", j5R, q6U);
  365. else if (!s)
  366. l9L("app_estr", j5R, q6U);
  367. else
  368. {
  369. size_t w6K = strlen(s);
  370. ok = TRUE;
  371. if ((es->g8G + w6K - 1) >= es->w6Y)
  372. STRPTR x9T = es->v7H; 
  373. if (k2Od(es,
  374. h5Z(es->g8G + w6K + 1, es->z3Lu), j5R, q6U))
  375. strcpy(es->v7H, 
  376. x9T);
  377. l5D(x9T); 
  378. }
  379. else
  380. ok = FALSE;
  381. }
  382. }
  383. if (ok)
  384. {
  385. STRPTR ds;
  386. ds = es->v7H + (es->g8G - 1);
  387. strcat(ds, s);
  388. es->g8G += w6K; 
  389. es->v7H[es->g8G - 1] = 0;
  390. }
  391. }
  392. return (ok);
  393. }
  394. BOOL k0O(x7H * dest, x7H * src, size_t v5N, size_t num)
  395. {
  396. BOOL ok = FALSE;
  397. x7H *j3J = s6Q(dest->z3Lu);
  398. if (j3J)
  399. {
  400. STRPTR x9T = j3J->v7H;
  401. if (v5N >= src->g8G)
  402. v5N = src->g8G - 1;
  403. if (v5N + num >= src->g8G)
  404. num = src->g8G - v5N - 1;
  405. ok = s0Y(j3J, h5Z(num + 1, j3J->z3Lu));
  406. if (ok)
  407. {
  408. strncpy(n5H(j3J), n5H(src) + v5N, num);
  409. j3J->v7H[num] = 0;
  410. j3J->g8G = num + 1;
  411. l5D(x9T);
  412. ok = f5J(dest, j3J);
  413. }
  414. p1X(j3J);
  415. }
  416. return (ok);
  417. }
  418. BOOL h5B(x7H * dest, x7H * src, size_t num)
  419. {
  420. if (num >= src->g8G)
  421. num = src->g8G - 1;
  422. return (k0O(dest, src, (src->g8G - num - 1), num));
  423. }
  424. BOOL z9F(x7H * dest, x7H * src, size_t num)
  425. {
  426. return (k0O(dest, src, 0, num));
  427. }
  428. STRPTR c2C(x7H * es)
  429. {
  430. return (es->v7H);
  431. }
  432. size_t z7Pg(x7H * es)
  433. {
  434. return (es->g8G - 1);
  435. }
  436. BOOL f5J(x7H * dest, x7H * src)
  437. {
  438. return (d1V(dest, n5H(src)));
  439. }
  440. BOOL e0C(x7H * dest, x7H * src)
  441. {
  442. return (o0S(dest, n5H(src)));
  443. }
  444.